package org.example.petsystem.controller;

import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.service.UserManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/UserManagement")
@CrossOrigin(origins = "*")
public class UserManController {
    @Autowired
    private UserManService userManService;
    @RequestMapping("/userManagement")
    public ResultBody userManagement() {
        if(userManService.UserMessage()!=null)
        {
            return new ResultBody(200,"显示用户信息成功",userManService.UserMessage(),userManService.UserMessage().size());
        }
        else
            return new ResultBody(400,"显示用户信息失败",false);
    }

    @RequestMapping("/updateUser")
    public ResultBody updateUser(@RequestBody Map<String,Object> map) {
        if(map.get("id")==null)
        {
            return new ResultBody(404,"缺少参数",false);
        }
        if(map.get("name")==null&& map.get("phonenumber")==null&& map.get("password")==null&& map.get("email")==null&& map.get("address")==null&& map.get("age")==null)
        {
            return new ResultBody(404,"缺少参数",false);
        }
        return userManService.updateUser(map);
    }

    @RequestMapping("/deleteUser")
    public ResultBody deleteUser(@RequestBody Map<String,Object>map) {
        if(map.get("phonenumber")==null)
        {
            return new ResultBody(404,"缺少参数",false);
        }
        String phonenumber= map.get("phonenumber").toString();
        if(userManService.deleteUser(phonenumber)==1)
            return new ResultBody(200,"删除成功",true);
        else
            return new ResultBody(400,"删除失败",false);
    }
}
